	package com.dj.uphome.web.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dj.common.web.controller.CoreCrudRestController;
import com.dj.common.web.exception.InvalidRequestException;
import com.dj.common.web.vo.EasyuiPage;
import com.dj.uphome.web.service.RestService;
import com.dj.uphome.web.shiro.ShiroUtil;

/**
 * 
 * 登录控制器
 *
 */
@Controller
public class IndexController extends CoreCrudRestController {

	@Autowired
	private RestService restservice;
	
	@RequestMapping({"/index","/"})
	public ModelAndView index() {
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("system/admin/index");
		return mv;
	}
	@RequestMapping(value = "/page/{url}")
	public ModelAndView pageUrl(@PathVariable("url") String url,String path,String method) {
		ModelAndView mv = this.getModelAndView();
		try {
			mv.addAllObjects(this.getParams());
			if (!StringUtils.isEmpty(url)) {		
				Map<String,Object> params = this.getParams();
				if(!StringUtils.isEmpty(path)&&!StringUtils.isEmpty(method)){
					String currentUserId = MapUtils.getString(params,"currentUserId");
					if(!StringUtils.isEmpty(currentUserId)){
						params.put(currentUserId, ShiroUtil.getUserId());
					}
					JSON json = restservice.callPostRequest("/"+path+"/"+method, params);
					mv.addObject("vo",json);
				}
				if(!StringUtils.isEmpty(MapUtils.getString(params,"requests"))){
					JSONObject jo = JSONObject.parseObject(MapUtils.getString(params,"requests"));
					Iterator<String> it = jo.keySet().iterator();
		            while (it.hasNext()) {
		            	Map<String,Object> params0 = new HashMap<>();
		                String key = (String) it.next();  
		                String value = jo.getString(key); 
		                if(!StringUtils.isEmpty(value)) {
		                	JSONObject jo0 = JSONObject.parseObject(value);
		                	if(null!=jo0) {
		                		Map<String,Object> map = jo0;
		                		params0.putAll(map);
		    					String currentUserId = MapUtils.getString(params0,"currentUserId");
		    					if(!StringUtils.isEmpty(currentUserId)){
		    						params0.put(currentUserId, ShiroUtil.getUserId());
		    					}
								JSON json = restservice.callPostRequest("/"+jo0.getString("url").replaceAll("-", "/"), params0);
								mv.addObject(key,json);
		                	}
		                }
		            }
				}
				url = url.replaceAll("-", "/");
				mv.setViewName(url);		
			} else {
				mv.setViewName("404");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("exception", e);
			mv.setViewName(defultError);
		}
		return mv;
	}
	@RequestMapping(value = "/post/{url}/{method}", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ResponseEntity<JSON> getJson(@PathVariable("url") String url,@PathVariable("method") String method) throws InvalidRequestException {
		Map<String,Object> params = this.getParams();
		String currentUserId = MapUtils.getString(params,"currentUserId");
		if(!StringUtils.isEmpty(currentUserId)){
			params.put(currentUserId, ShiroUtil.getUserId());
		}
		return new ResponseEntity<>(restservice.callPostRequest("/"+url+"/"+method, params),HttpStatus.OK);
	}
	@RequestMapping(value = "/datagrid/{url}/{method}", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ResponseEntity<EasyuiPage> datagrid(EasyuiPage page,@PathVariable("url") String url,@PathVariable("method") String method) throws InvalidRequestException {
		Map<String,Object> params = this.getParams();
		page.setParams(params);
		String currentUserId = MapUtils.getString(params,"currentUserId");
		if(!StringUtils.isEmpty(currentUserId)){
			params.put(currentUserId, ShiroUtil.getUserId());
		}
		JSONObject jo =  (JSONObject)restservice.callPostRequest("/"+url+"/"+method, params);
		return new ResponseEntity<>(new EasyuiPage(jo),HttpStatus.OK);
	}
}
